package org.xs.web.http;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.xs.web.http.provider.HTTPContext;
import org.xs.web.http.provider.HTTPFilter;
import org.xs.web.http.provider.HTTPRequest;
import org.xs.web.http.provider.HTTPResponse;
import org.xs.web.http.provider.HTTPSession;
import org.xs.web.util.SMap;

 
public class S {
	private static final ThreadLocal<Boolean> isStart = new ThreadLocal<Boolean>();
	private static final ThreadLocal<Map<String, Object>> contextTL =new ThreadLocal<Map<String,Object>>();
	private static final ThreadLocal<SMap<String, Object>> params =new ThreadLocal<SMap<String, Object>>();
	private static final ThreadLocal<Flash> flash = new ThreadLocal<Flash>();
	private static final ThreadLocal<XSContext> xsContext = new ThreadLocal<XSContext>();
	private static final ThreadLocal<HTTPRequest> request = new ThreadLocal<HTTPRequest>();
	private static final ThreadLocal<HTTPResponse> response = new ThreadLocal<HTTPResponse>();
	private static final ThreadLocal<HTTPSession> session = new ThreadLocal<HTTPSession>();
	private static final ThreadLocal<HTTPContext> context = new ThreadLocal<HTTPContext>();
	private static final ThreadLocal<ApplicationContext> appCtx = new ThreadLocal<ApplicationContext>();
	
	public static void init(Object other ,HTTPRequest req,HTTPResponse resp,HTTPContext ctx,ApplicationContext appctx){
		if(!(other instanceof HTTPFilter)){
			throw new IllegalStateException("S has be settting");
		}else{
			isStart.set(true);
			request.set(req);
			response.set(resp);
			session.set(req.session()); 
			context.set(ctx);
			appCtx.set(appctx);
			contextTL.set(Collections.synchronizedMap(new HashMap<String, Object>()));
			flash.set(Flash.create());
			xsContext.set(XSContext.create());
			params.set( new SMap<String, Object>(req.params()));
		}
	}
	public static Map<String, Object>  getContext(){
		if(!isStart()){
			return null;
		}
		assert(contextTL != null);
		return contextTL.get();
	}
	 
	
	public static boolean isStart(){
		return isStart.get() == null ? false : isStart.get();
	}
	
	public static Object getBean(String name){
		try {
			return getAppContext().getBean(name);
		} catch (Exception e) {
			 
		}
		return null;
	}
	
	public static ApplicationContext getAppContext(){
		if(!isStart()){
			return null;
		}
		assert(appCtx.get() != null);
		return appCtx.get();
	}
	
	public static void set(String key,Object value){
		getContext().put(key, value);
	}
	
	public static String[] params(String name){
		Object value = params().get(name);
		if(value == null){
			return new String[]{};
		}
		if(value.getClass().isArray()){
			return (String[]) value;
		}
		return new String[]{(String)value}; 
	}
	
	public static SMap<String, Object> params(){
		return params.get();
	}
	
	public static HTTPSession session(){
		if(session.get() == null){
			throw new IllegalStateException("HTTPSession is null");
		}
		return session.get();
	}
	
	public static HTTPResponse response(){
		if(response.get() == null){
			throw new IllegalStateException("HTTPResonse is null");
		}
		return response.get();
	}
	
	public static HTTPRequest request(){
		if(request.get() == null ){
			throw new IllegalStateException("HTTPRequest is null");
		}
		return request.get();
	}
	
	public static Flash flash(){
		return flash.get();
	}
	
	public static XSContext xsContext(){
		return xsContext.get();
	}
	
	public static void shutdown(Object other){
		if(other instanceof HTTPFilter){
			isStart.set(false);
			params.set(null);
			flash.set(null);
			xsContext.set(null);
			contextTL.set(null);
			context.set(null);
			request.set(null);
			response.set(null);
			session.set(null);
			appCtx.set(null);
		}
	}
	
	
	
	public static boolean isJson(){
		return request().isJson();
	}
	
	public static boolean isHtml(){
		return request().isHtml();
	}
	
	public static String referer(){
		String referer = request().getHeader("Referer");
		return referer;
	}
	
	
	
	
	
	
	
	
	
	
	
	
}
